package com.activemanager.myHealthProfile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.activemanager.dashboard.H0Activity;
import com.activemanager.dashboard.H2Activity;
import com.activemanager.digitalTrain.WelcomeCalibrating_DT0;
import com.example.android.navigationdrawerexample.R;
import com.example.android.navigationdrawerexample.R.drawable;
import com.example.android.navigationdrawerexample.R.id;
import com.example.android.navigationdrawerexample.R.layout;

import android.app.Activity;
import android.app.Dialog;
import android.app.ActionBar.LayoutParams;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class Q1MainActivity extends Activity {
	TextView textView;
	ImageView imageButton1;
	Button button1, button2, button3, button4, button5;
	String tt = null;
	private static String TAG = "HealthProfileActivity";

	// ����ͼƬ
	private ImageView imageView_qi;

	// ����ѡ��8
	private SeekBar seekBar_time;

	// ���е�ͼƬ
	private Integer[] bitmapIdArray;
	private Drawable[] drawableArray;
	private Drawable thumbDown;
	private Drawable thumbUp;
	private Integer[] sportBitmapIdArray;
	private Drawable[] sportDrawableArray;

	// ��ʾ����
	private String[] strArray;
	
	private String[] []str;
	private TextView[] textViewArray;
	private TextView textView_time;

	private RelativeLayout box_text;

	private LinearLayout linearLayout_sport;

	private int user_time_id = 1;
	
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.q1);
		// View rootView = inflater.inflate(R.layout.q1, container, false);

		initPic();
		
        ImageView img_home = (ImageView) findViewById(R.id.imageView);
		img_home.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}});
		
		
		RelativeLayout chooseLayout = (RelativeLayout) findViewById(R.id.choose_Layout2);
		textView = (TextView) findViewById(R.id.textView_choose);
		imageButton1 = (ImageView) findViewById(R.id.imageView6);
		button1 = (Button) findViewById(R.id.rhythm);
		button2 = (Button) findViewById(R.id.force);
		button3 = (Button) findViewById(R.id.active);
		button4 = (Button) findViewById(R.id.workout);
		button5 = (Button) findViewById(R.id.speed);
		final Intent intent = new Intent();
		intent.setClass(Q1MainActivity.this, Q1Activity.class);
		button1.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				intent.putExtra("pos", 4 );
				startActivity(intent);
			}
		});
		
		button2.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				intent.putExtra("pos", 1 );
				startActivity(intent);
			}
		});
		
		button3.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				intent.putExtra("pos", 2 );
				startActivity(intent);
			}
		});
		
		
		button4.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				intent.putExtra("pos", 3 );
				startActivity(intent);
			}
		});
		
		button5.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				intent.putExtra("pos", 0 );
				startActivity(intent);
			}
		});
		
		Button btn_share = (Button) findViewById(R.id.btn_share);
		btn_share.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				final Dialog dialog = new Dialog(Q1MainActivity.this,
						R.style.MyDialog);
				
				dialog.setContentView(R.layout.p7_share);
				
				final BitmapFactory.Options opt = new BitmapFactory.Options();
				opt.inPreferredConfig = Bitmap.Config.RGB_565;
				opt.inPurgeable = true;
				opt.inInputShareable = true;
				
				final LinearLayout img = (LinearLayout) dialog.findViewById(R.id.linearLayout);
//				InputStream is = getResources().openRawResource(
//						R.drawable.u165_normal);
//				Bitmap bit = BitmapFactory.decodeStream(is, null, opt);
//				img.setBackgroundDrawable(new BitmapDrawable(bit));
				img.setBackgroundResource(R.drawable.bg_for_popup_54);
				
				
				dialog.show();
				Button btnDialog_cancel = (Button) dialog
						.findViewById(R.id.button1);
				btnDialog_cancel.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						dialog.dismiss();	
					}
				});
				Window dialogWindow = dialog.getWindow();
				WindowManager m = getWindowManager();
				Display d = m.getDefaultDisplay();
				WindowManager.LayoutParams p = dialogWindow.getAttributes();
				p.height = (int) (d.getHeight() * 0.765);
				p.width = (int) (d.getWidth() * 0.95);
				dialogWindow.setAttributes(p);
				
				String[] mFrom = new String[] { "profile_img", "profile_item" };
				String[] profileTitle = { "Sina Weibo", "Tencent Weibo", "We Chat",
						"QQ", "KaiXin", "Email" };
				int[] mTo = new int[] { R.id.profile_img, R.id.profile_item };
				List<Map<String, Object>> mList = new ArrayList<Map<String, Object>>();
				Map<String, Object> mMap = null;
				for (int i = 0; i < profileTitle.length; i++) {
					mMap = new HashMap<String, Object>();
					
					int drawable = getResources().getIdentifier(
							"share_to_" + (1 + i), "drawable",
							Q1MainActivity.this.getPackageName());
					mMap.put("profile_img", drawable);
					mMap.put("profile_item", profileTitle[i]);
					mList.add(mMap);
				
					
				}
				final ListView mListView = (ListView) dialog.findViewById(R.id.list_view);
				SimpleAdapter mAdapter = new SimpleAdapter(Q1MainActivity.this, mList,	R.layout.drawer_list_item_for_sports, mFrom, mTo);
				mListView.setAdapter(mAdapter);
				mListView.setOnItemClickListener(new OnItemClickListener(){

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int pos, long arg3) {
						// TODO Auto-generated method stub
							dialog.dismiss();
							final Dialog dialog1 = new Dialog(Q1MainActivity.this,
									R.style.MyDialog);
							dialog1.setContentView(R.layout.onloading_error);
							Window dialogWindow = dialog1.getWindow();
							WindowManager m = getWindowManager();
							Display d = m.getDefaultDisplay();
							WindowManager.LayoutParams p = dialogWindow.getAttributes();
							p.height = (int) (d.getHeight() * 0.47);
							p.width = (int) (d.getWidth() * 0.95);
							dialogWindow.setAttributes(p);
							final RelativeLayout img = (RelativeLayout) dialog1.findViewById(R.id.d_share_layout);
							final TextView tv = (TextView) dialog1.findViewById(R.id.textView1);
							InputStream is = getResources().openRawResource(
									R.drawable.u165_normal);
							Bitmap bit = BitmapFactory.decodeStream(is, null, opt);
							img.setBackgroundDrawable(new BitmapDrawable(bit));
							dialog1.show();
								new Handler().postDelayed(new Runnable(){
								
								@Override
								public void run() {
									tv.setText("The information has been\n   successfully shared.");
									new Handler().postDelayed(new Runnable(){
										
										@Override
										public void run() {
											dialog1.dismiss();
										}}, 1000);
									
								}}, 800);
						

					}
					
				});

			
			}
			});
		
	

		chooseLayout.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {

				final Dialog dialog = new Dialog(Q1MainActivity.this,
						R.style.MyDialog);
				
				dialog.setContentView(R.layout.p7);
				
				BitmapFactory.Options opt = new BitmapFactory.Options();
				opt.inPreferredConfig = Bitmap.Config.RGB_565;
				opt.inPurgeable = true;
				opt.inInputShareable = true;
				
				final LinearLayout img = (LinearLayout) dialog.findViewById(R.id.linearLayout);
				InputStream is = getResources().openRawResource(
						R.drawable.u165_normal);
				Bitmap bit = BitmapFactory.decodeStream(is, null, opt);
				img.setBackgroundDrawable(new BitmapDrawable(bit));
//				img.setBackgroundResource(R.drawable.u165_normal);
				
				
				dialog.show();
				Window dialogWindow = dialog.getWindow();
				WindowManager m = getWindowManager();
				Display d = m.getDefaultDisplay();
				WindowManager.LayoutParams p = dialogWindow.getAttributes();
				p.height = (int) (d.getHeight() * 0.765);
				p.width = (int) (d.getWidth() * 0.95);
				dialogWindow.setAttributes(p);
				
				String[] mFrom = new String[] {"choose_item"};
				int[]  mTo =new int[] {  R.id.profile_item};
				String[] chooseTitle = { "Hour", "Day", "Week", "Month", "Activity" };
				List mList = new ArrayList<String>();
				for (int i = 0; i < chooseTitle.length; i++) {
					Map<String, Object> mMap = new HashMap<String,Object>();
					mMap.put("choose_item", chooseTitle[i]);
					mList.add(mMap);
				}
				final ListView mListView = (ListView) dialog.findViewById(R.id.list_view);
				SimpleAdapter mAdapter = new SimpleAdapter(Q1MainActivity.this, mList,	R.layout.pure_text_list_item, mFrom, mTo);
				mListView.setAdapter(mAdapter);
				mListView.setOnItemClickListener(new OnItemClickListener(){

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int pos, long arg3) {
						// TODO Auto-generated method stub
						if(pos!=0 && pos != 4){
						HashMap<String,String> map = (HashMap<String, String>) mListView.getItemAtPosition(pos);
						String text = map.get("choose_item");
						textView.setText("By "+text.split("\\(")[0]);
						
						//System.arraycopy(strArray1, 0, strArray, 0, 8);
						switch(pos)
						{
						case 1:strArray=new String[]{ "Mon", "Tue", "Wed", "Thu", "Fri", "Sta", "Sun", "Today"};
							break;
						case 2:strArray= new String[]{"w38","w39","w40","w41","w41","w42","w43","w44"};
							break;
						case 3:strArray=new String[]{"Mar","Apr","May","Jun","Jul","Aug","Sep","Oct"};
							break;
						}
							for (int i = 0; i < strArray.length; i++) {
							
							textViewArray[i].setText(strArray[i]);
						}
						dialog.dismiss();
						}
					}
					
				});

				
				Button btnDialog_Ok = (Button) dialog
						.findViewById(R.id.button1);
				btnDialog_Ok.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						dialog.dismiss();	
					}
				});
//
			}
		});

	
		imageView_qi = (ImageView) findViewById(R.id.imageView3);
		seekBar_time = (SeekBar) findViewById(R.id.seekBar_time);
		box_text = (RelativeLayout) findViewById(R.id.box_text);
		textView_time = (TextView) findViewById(R.id.textView_time);
		linearLayout_sport = (LinearLayout) findViewById(R.id.linearLayout_sport);

		bitmapIdArray = new Integer[] { R.drawable.qi1, R.drawable.qi2,
				R.drawable.qi3, R.drawable.qi4, R.drawable.qi5, R.drawable.qi6,
				R.drawable.qi7, R.drawable.qi8 };
		sportBitmapIdArray = new Integer[] { R.drawable.badmintion_c,
				R.drawable.basketball_c, R.drawable.cycling_c,
				R.drawable.digi_c, R.drawable.golf_c,
				R.drawable.gym_c };
		strArray = new String[] { "Mon", "Tue", "Wed", "Thu", "Fri", "Sta", "Sun", "Today"};
		
		// ��ͼƬ���뻺�棬����ٶ�
		drawableArray = new Drawable[bitmapIdArray.length];
		for (int i = 0; i < bitmapIdArray.length; i++) {
			BitmapFactory.Options opt = new BitmapFactory.Options();
			opt.inPreferredConfig = Bitmap.Config.RGB_565;
			opt.inPurgeable = true;
			opt.inInputShareable = true;
			InputStream is = getResources().openRawResource(
					bitmapIdArray[i]);
			Bitmap bgbit = BitmapFactory.decodeStream(is, null, opt);
			drawableArray[i] = new BitmapDrawable(bgbit);
		}
		sportDrawableArray = new Drawable[sportBitmapIdArray.length];
		for (int i = 0; i < sportBitmapIdArray.length; i++) {
			sportDrawableArray[i] = getResources().getDrawable(sportBitmapIdArray[i]);
		}
		thumbDown = getResources().getDrawable(R.drawable.cursor_down);
		thumbUp = getResources().getDrawable(R.drawable.cursor_up);
		textViewArray = new TextView[strArray.length];
		for (int i = 0; i < strArray.length; i++) {
			textViewArray[i] = new TextView(this);
			textViewArray[i].setText(strArray[i]);
		}
		
		updateTimeView();
	
		updateSportView();
		
		seekBar_time.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override
			public void onProgressChanged(SeekBar arg0, int index, boolean arg2) {
				
				if(user_time_id!=indexToId(index))
				{
					System.out.println("&&&&&"+index);
					updateSportView();
				}
				user_time_id = indexToId(index);
				// Log.i(TAG,"index:"+index+"     id:"+user_time_id);
				setTime(false);
			}

			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				addTimeView();
				seekBar_time.setThumb(thumbDown);
			}

			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				box_text.removeAllViews();
				seekBar_time.setThumb(thumbUp);
				setTime(true);
			}
		});
	}

	private void initPic() {
		// TODO Auto-generated method stub
		BitmapFactory.Options opt = new BitmapFactory.Options();
		opt.inPreferredConfig = Bitmap.Config.RGB_565;
		opt.inPurgeable = true;
		opt.inInputShareable = true;
		
		ImageView img = (ImageView) findViewById(R.id.imageView1);
		InputStream is = getResources()
				.openRawResource(R.drawable.u0_normal_star);
		Bitmap bit = BitmapFactory.decodeStream(is, null, opt);
		img.setImageBitmap(bit);


		ImageView img2 = (ImageView) findViewById(R.id.imageView3);
		InputStream is2 = getResources().openRawResource(R.drawable.u30_normal);
		Bitmap bit2 = BitmapFactory.decodeStream(is2, null, opt);
		img2.setImageBitmap(bit2);
//		
		LinearLayout img3 = (LinearLayout) findViewById(R.id.linearLayout1);
//		InputStream is3 = getResources().openRawResource(
//				R.drawable.background_normal);
//		Bitmap bit3 = BitmapFactory.decodeStream(is3, null, opt);
//		img3.setBackgroundDrawable(new BitmapDrawable(bit3));
		img3.setBackgroundResource(R.drawable.bg_540);
		
		
	}
	private void setTime(boolean isSetBar) {
		if (isSetBar) {
			seekBar_time.setProgress(idToIndex(user_time_id));
		}
		// imageView_qi.setImageResource(bitmapIdArray[user_time_id]);
		imageView_qi.setImageDrawable(drawableArray[user_time_id]);
		updateTimeView();
	}

	/**
	 * �ڻ���������ʾʱ��
	 */
	private void addTimeView() {
		box_text.removeAllViews();
		double w = box_text.getWidth() * 1.0 / (textViewArray.length);
		for (int i = 0; i < textViewArray.length; i++) {
			RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			textViewArray[i].setLayoutParams(lp1);
			textViewArray[i].setPadding((int) (w * (i + 0.2)), 0, 0, 0);
			textViewArray[i].setTextColor(Color.WHITE);
			box_text.addView(textViewArray[i], lp1);
		}
	}

	/**
	 * ����������ɫ
	 */
	private void updateTimeView() {
		textView_time.setText("Oct " + Integer.toString(user_time_id + 1) + ",2013");

		for (int i = 0; i < strArray.length; i++) {
			textViewArray[i].setTextColor(Color.BLACK);
		}
		textViewArray[user_time_id].setTextColor(Color.RED);
	}

	/**
	 * ����sport��ʾ�б�
	 */
	private void updateSportView() {
		linearLayout_sport.removeAllViews();
		int sport_num = (int) (Math.random() * sportDrawableArray.length/2) + 1;
		for (int i = 0; i < sport_num; i++) {
			View sportItem = View.inflate(this, R.layout.health_sport_item,
					null);
			TextView textView_sport_times = (TextView) sportItem
					.findViewById(R.id.textView_sport_times);
			textView_sport_times.setText("x "
					+ Integer.toString((int) (Math.random() * sportDrawableArray.length/2) + 1));
			ImageView imageView_sport = (ImageView) sportItem
					.findViewById(R.id.imageView_sport);
			imageView_sport.setImageDrawable(sportDrawableArray[(int) (Math.random() * sportDrawableArray.length)]);
			linearLayout_sport.addView(sportItem);
		}
	}

	private int indexToId(int index) {
		return (index+5) / 10;
	}

	private int idToIndex(int id) {
		return id * 10;
	}
}
